﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using ArgusLib.Drawing;

namespace ArgusLib.Controls
{
	public class ImageCollection : ObjectCollection<Image>
	{
		Size _imageSize;
		public int MinWidth { get; private set; }
		public int MaxWidth { get; private set; }
		public int MinHeight { get; private set; }
		public int MaxHeight { get; private set; }

		public Size ImageSize
		{
			get { return this._imageSize; }
			set
			{
				if (this._imageSize != value)
				{
					this._imageSize = value;
					this.OnImageSizeChanged(EventArgs.Empty);
				}
			}
		}

		public event EventHandler ImageSizeChanged;

		public ImageCollection()
			: base()
		{
			this.ImageSize = Size.Empty;
		}

		protected virtual void OnImageSizeChanged(EventArgs e)
		{
			if (this.ImageSizeChanged != null)
				this.ImageSizeChanged(this, e);
		}

		protected override void OnItemAdded(ItemEventArgs e)
		{
			if (this.ImageSize.IsEmpty == false)
			{
				if ((this.ImageSize.Width > 0 && e.Item.Size.Width > this.ImageSize.Width) || (this.ImageSize.Height > 0 && e.Item.Size.Height > this.ImageSize.Height))
				{
					double r = e.Item.Size.GetRatio();
					Size size = this.ImageSize;
					if (size.Width < 1)
						size.Width = int.MaxValue;
					if (size.Height < 1)
						size.Height = int.MaxValue;
					size = size.ChangeRatio(r, ChangeRatioOptions.DecreaseAreaSize);
					Bitmap bitmap = new Bitmap(size.Width, size.Height);
					using (Graphics g = Graphics.FromImage(bitmap))
					{
						g.DrawImage(
							e.Item,
							bitmap.GetRectangle(),
							e.Item.GetRectangle(),
							GraphicsUnit.Pixel);
					}
					this[e.Index] = bitmap;
					e = new ItemEventArgs(e.Index, e.Key, bitmap);
				}
			}
			base.OnItemAdded(e);
			if (e.Item != null)
			{
				if (this.MinWidth == 0 || e.Item.Width < this.MinWidth)
					this.MinWidth = e.Item.Width;
				if (this.MaxWidth == 0 || e.Item.Width > this.MaxWidth)
					this.MaxWidth = e.Item.Width;
				if (this.MinHeight == 0 || e.Item.Height < this.MinHeight)
					this.MinHeight = e.Item.Height;
				if (this.MaxHeight == 0 || e.Item.Height > this.MaxHeight)
					this.MaxHeight = e.Item.Height;
			}
		}

		protected override void OnItemRemoved(ItemEventArgs e)
		{
			base.OnItemRemoved(e);
			if (e.Item != null)
			{
				if (this.MinWidth == e.Item.Width)
				{
					this.MinWidth = int.MaxValue;
					foreach (Image image in this)
					{
						if (image != null && image.Width < this.MinWidth)
							this.MinWidth = image.Width;
					}
				}
				if (this.MaxWidth == e.Item.Width)
				{
					this.MaxWidth = 0;
					foreach (Image image in this)
					{
						if (image != null && image.Width > this.MaxWidth)
							this.MaxWidth = image.Width;
					}
				}
				if (this.MinHeight == e.Item.Height)
				{
					this.MinHeight = int.MaxValue;
					foreach (Image image in this)
					{
						if (image != null && image.Height < this.MinHeight)
							this.MinHeight = image.Height;
					}
				}
				if (this.MaxHeight == e.Item.Height)
				{
					this.MaxHeight = 0;
					foreach (Image image in this)
					{
						if (image != null && image.Height > this.MaxHeight)
							this.MaxHeight = image.Height;
					}
				}
			}
		}
	}
}
